<?php 
/**
 *用户微博登陆验证类
 *@author [cnsecer] <[root@cnsecer.com]>
 * 
 */
namespace Home\Controller; 
use Think\Controller;
//微博登陆授权回调函数
class WeiboController  extends CommonController {

    //回调函数
    public function callback(){
        import('Util.saetv2#ex',APP_PATH);
        $o = new \Util\SaeTOAuthV2( C('WB_AKEY') , C('WB_SKEY') );
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = C('WB_CALLBACK_URL');
            try {
                $token = $o->getAccessToken( 'code', $keys ) ;
            } catch (OAuthException $e) {
            }
            if ($token) {
                $db = M('user');
                $id = session('id'); //获取本地用户uid
                $_SESSION['token'] = $token;
                $data['access_token'] = $_SESSION['token']['access_token']; //获取微博用户令牌
                setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );  //如果用户已经登陆  把token存到数据库
                $result = $db ->where(array('id'=>$id))->save($data); //更新字段信息
                $this->show('授权完成,<a href="__APP__/">进入你的微博列表页面</a><br />');
              //  dump($db->select());
            }else{
                $this->show('授权失败');
            }
        }
    }
    //用户信息
    public function userinfo(){
        $c   = $this->c;   
        $uid = $this->uid; //微博用户UID
        //把数据传递到模板        
        $this->user_message = $c->show_user_by_id( $uid);
        //本地用户信息
        $db = M('user');
        $this->userInfo = $db->where(array('access_token'=>$_SESSION['token']['access_token']))->select();
        $this->display();
    }
    //修改密码
    public function repsw(){
        $this->display();
    }
    //修改密码处理表单
    public function repswHandle(){

      //  if(!IS_POST) $this->error("非法操作!");
        $rules = array(        
             array('repassword','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致     
             array('password','checkPwd','密码格式不正确',0,'function'), // 自定义函数验证密码格式

         ); 

         $data['password'] =I('password','','md5');
     //    $data['repassword'] =I('repassword','','md5');
         $data['id'] =session('id');
       //  dump($data);die;
         $user = M('user');
         if(!isset($data['id'])){
            $this->error("请使用本地账号登陆!");
         }else{ 
             if (!$user->validate($rules)->create()){
                $this->error("密码修改失败:".$user->getError());  
             } else{
                $user->save($data);
                $this->success("密码修改成功");
             }  
         }   
    }
    //留言板
    public function guestbook(){
        $this->display();
    }
    //关于
    public function about(){
        $this->display();
    }

}
 ?>